package edu.imac.angryfood.render;
/* ====================================== */
/**USELESS */
/* ====================================== */
// pas fait encore
import org.jbox2d.common.Vec2;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import edu.imac.angryfood.shapes.Circle;
import edu.imac.angryfood.shapes.CompoundShape;
import edu.imac.angryfood.shapes.Polygon;
import edu.imac.angryfood.shapes.Rectangle;
import edu.imac.angryfood.shapes.Shape;
import edu.imac.angryfood.world.Body;

public class ShapesRender {
	
	/** type of rendering : 1=contour 2=contour +img 3=img alone */
	private int renderMode;
	
	public ShapesRender(int renderMode) {
		super();
		 this.renderMode = renderMode;
	}

	/**
	 * draws a segment
	 * @param g graphics
	 * @param x1 x coordinate of the 1st point
	 * @param y1 y coordinate of the 1st point
	 * @param x2 x coordinate of the 2nd point
	 * @param y2 y coordinate of the 2nd point
	 */
	public void drawSegment(Graphics g, float x1, float y1, float x2, float y2) {
		g.drawLine(x1, y1, x2, y2);
	}
	
	/**
	 * draws the axis at the center
	 * @param g graphics
	 */
	public void drawAxes(Graphics g) {
		g.drawLine(0, 0, 0, 10);
		g.drawLine(0, 0, 10, 0);
	}

	/** identify the shape of the body
	 * 
	 * @param g graphics
	 * @param body the body to draw
	 */
	public void drawBody(Graphics g, Body body) {
		if(body.isShow()) {
			Shape shape = body.getShape();
			if (body.getImg() == null && this.renderMode >= 2) 
				drawShape(g, body, shape, 1);
			else if(body.getImg() == null && this.renderMode <= 1)
				drawShape(g, body, shape, this.renderMode);
			else
				drawShape(g, body, shape, this.renderMode);
		}
	}

	/**
	 * define of what shape is composed the body and launch the good function
	 * @param g graphics
	 * @param body the body of the shape
	 * @param shape the shape to draw
	 * @param renderMode the render mode
	 */
	private void drawShape(Graphics g, Body body, Shape shape,int renderMode) {
		if (shape instanceof Rectangle) {
			drawRectangle(g, body, (Rectangle) shape,renderMode);
		}
		if (shape instanceof CompoundShape) {
			drawCompound(g, body, (CompoundShape) shape,renderMode);
		}
		if (shape instanceof Circle) {
			drawCircle(g, body, (Circle) shape,renderMode);
		}
		if (shape instanceof Polygon) {
			drawPolygon(g, body, (Polygon) shape,renderMode);
		}
	}

	/**
	 * draw a rectangle
	 * @param g graphics
	 * @param body the body of the rectangle
	 * @param shape the shape of the rectangle
	 * @param renderMode the render mode
	 */
	private void drawRectangle(Graphics g, Body body, Rectangle shape, int renderMode) {
		g.pushTransform();
		g.setColor(Color.red);
			g.translate(body.getX(), body.getY());
			g.rotate(0, 0, (float) Math.toDegrees(body.getRotation()));
			g.translate(shape.getXOffset(), shape.getYOffset());
			g.rotate(0, 0, (float) Math.toDegrees(shape.getAngleOffset()));
	
			float width = shape.getWidth();
			float height = shape.getHeight();
			if (renderMode == 0) 
				g.fill(body.getSlickShape());
			if (renderMode == 1) 
				g.draw(body.getSlickShape());
			if (renderMode == 2) {
				g.draw(body.getSlickShape());
				g.drawImage(body.getImg(), (int) -(width/2),(int) -(height/2));
			}
			if (renderMode  == 3) 
				g.drawImage(body.getImg(), (int) -(width/2),(int) -(height/2));
		g.popTransform();
	}

	/**
	 * draw a circle
	 * @param g graphics
	 * @param body the body of the rectangle
	 * @param shape the shape of the rectangle
	 * @param renderMode the render mode
	 */
	public void drawCircle(Graphics g, Body body, Circle shape, int renderMode) {
		g.pushTransform();
			g.setColor(Color.blue);
			g.translate(body.getX(), body.getY());
			g.rotate(0, 0, (float)Math.toDegrees(body.getRotation()));
			
			float radius = shape.getRadius();	
			if (renderMode == 0) 
				g.fill(body.getSlickShape());
			if (renderMode == 1) {
				g.draw(body.getSlickShape());
				g.drawLine(0, 0, 0, (int) -radius);
			}
			if (renderMode == 2) {
				g.draw(body.getSlickShape());
				g.drawImage(body.getImg(),(int) -radius,(int) -radius);
				g.drawLine(0, 0, 0, (int) -radius);
			}
			if (renderMode  == 3) 
				g.drawImage(body.getImg(),(int) -radius,(int) -radius);
			
		g.popTransform();
	}
	

	/**
	 * draw a polygon
	 * @param g graphics
	 * @param body the body of the rectangle
	 * @param shape the shape of the rectangle
	 * @param renderMode the render mode
	 */
	private void drawPolygon(Graphics g, Body body, Polygon shape,int renderMode) {
		g.pushTransform();

			g.setColor(Color.yellow);
			g.translate(body.getX(), body.getY());
			g.rotate(0,0,(float)Math.toDegrees(body.getRotation()));
			int vertexCount = shape.getDef().getVertexCount();
			Vec2[] vertices = shape.getDef().getVertexArray();
		
				float[] points = new float[vertexCount * 2];
				for (int i = 0; i < vertexCount; i++) {
					Vec2 screen = vertices[i];
					points[2 * i] = screen.x;
					points[2 * i + 1] = screen.y;
				}
				org.newdawn.slick.geom.Polygon poly = new org.newdawn.slick.geom.Polygon(points);
				if (renderMode  == 3) 
					g.drawImage(body.getImg(), 0,0);
				if (renderMode == 2) {
					g.drawImage(body.getImg(), 0,0);
					g.draw(poly);
				}
				if (renderMode == 1) {
					g.draw(poly);
				}
				if ( renderMode == 0)
					g.fill(poly);		
			
		g.popTransform();
	}
	
	/**
	 * to draw a compound body
	 * @param g graphics
	 * @param body the body of the rectangle
	 * @param shape the shape of the rectangle
	 * @param renderMode the render mode
	 */
	private void drawCompound(Graphics g, Body body, CompoundShape shape, int renderMode) {
		int count = shape.getShapeCount();
		// when we draw a compound shape we're just draw each of the sub-shapes
		for (int i = 0; i < count; i++) {
			drawShape(g, body, shape.getShape(i),renderMode);
		}
	}

}
